package com.xbongbong.paas.service.stage.impl;

import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.pojo.vo.StageGetVO;
import com.xbongbong.paas.service.stage.AbstractStageSettingsStrategy;
import com.xbongbong.paas.service.stage.StageSettingsStrategy;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CustomerStageErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.StageWorkEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 取消阶段设置策略
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/11/26 18:16
 */
@Service("cancelStageSettingsStrategy")
public class CancelStageSettingsStrategy extends AbstractStageSettingsStrategy implements StageSettingsStrategy {

    @Override
    public Integer type() {
        return StageTypeEnum.CANCEL_STAGE.getType();
    }

    @Override
    public StageGetVO get(PaasStageEntity paasStageEntity, List<StageWorkEntity> stageWorkEntityList) throws XbbException {
        return wrapStageEntity(paasStageEntity);
    }

    @Override
    public void check(List<PaasStageEntity> paasStageEntityList, Integer businessType) throws XbbException {
        //销售机会业务
        if (Objects.nonNull(businessType) && (Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.ALL_OPPORTUNITY.getCode()))) {
            if (CollectionsUtil.isNotEmpty(paasStageEntityList) && paasStageEntityList.size() > BasicConstant.ONE) {
                throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238053);
            }
            for (PaasStageEntity paasStageEntity : paasStageEntityList) {
                paasStageEntity.setIntoProportion(paasStageEntity.getOutProportion());
            }
        }
    }
}
